package com.run.cat.api.controller;


import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.run.cat.common.base.BaseResult;
import com.run.cat.common.enums.IntegerEnum;
import com.run.cat.common.util.EmptyUtil;
import com.run.cat.dao.entity.Image;
import com.run.cat.dao.entity.User;
import com.run.cat.dao.query.UserQuery;
import com.run.cat.service.IImageService;
import com.run.cat.service.IUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

/**
 * <p>
 * 用户信息表 前端控制器
 * </p>
 *
 * @author CG
 * @since 2020-08-19
 */
@Controller
@RequestMapping("/user")
public class UserController {

    @Autowired
    private IUserService userService;
    @Autowired
    private IImageService imageService;

    @RequestMapping("/list")
    @ResponseBody
    public BaseResult queryUserList(@RequestBody UserQuery query){
        User loginUser = userService.getUser();
        if(EmptyUtil.isEmpty(loginUser)) return BaseResult.failMsg("当前用户未登录");
        EntityWrapper<User> wrapper = new EntityWrapper<>();
        wrapper.eq("sex",loginUser.getSex().equals("女")?"男":"女");
        Page<User> page = userService.queryPage(wrapper,query.getCurrent(),query.getSize());
        page.getRecords().stream().forEach(user -> {
            List<Image> images = imageService.queryList(
                    new Image().setUserId(user.getId()).setType(IntegerEnum.img_type_1.getCode())
            );
            user.setImageList(images);
        });
        return BaseResult.successData(page);
    }

    @RequestMapping("/detail")
    @ResponseBody
    public BaseResult detail(@RequestBody User user){
        User searchUser = userService.selectById(user.getId());
        List<Image> images = imageService.queryList(
                new Image().setUserId(searchUser.getId()).setType(IntegerEnum.img_type_1.getCode())
        );
        searchUser.setImageList(images);
        return BaseResult.successData(searchUser);
    }

}
